package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SupplierMaterialMapper;
import com.ruoyi.system.domain.SupplierMaterial;
import com.ruoyi.system.service.ISupplierMaterialService;

import javax.annotation.Resource;

/**
 * 供应商物料库存Service业务层处理
 *
 * @author 协同
 * @date 2022-04-18
 */
@Service
public class SupplierMaterialServiceImpl implements ISupplierMaterialService {
    @Resource
    private SupplierMaterialMapper supplierMaterialMapper;

    /**
     * 查询供应商物料库存
     *
     * @param id 供应商物料库存主键
     * @return 供应商物料库存
     */
    @Override
    public SupplierMaterial selectSupplierMaterialById(Long id) {
        return supplierMaterialMapper.selectSupplierMaterialById(id);
    }

    /**
     * 查询供应商物料库存列表
     *
     * @param supplierMaterial 供应商物料库存
     * @return 供应商物料库存
     */
    @Override
    public List<SupplierMaterial> selectSupplierMaterialList(SupplierMaterial supplierMaterial) {
        supplierMaterial.setSupplierId(SecurityUtils.getLoginUser().getUserId());
        return supplierMaterialMapper.selectSupplierMaterialList(supplierMaterial);
    }

    /**
     * 新增供应商物料库存
     *
     * @param supplierMaterial 供应商物料库存
     * @return 结果
     */
    @Override
    public int insertSupplierMaterial(SupplierMaterial supplierMaterial) {
        supplierMaterial.setSupplierId(SecurityUtils.getLoginUser().getUserId());
        supplierMaterial.setCreateTime(DateUtils.getNowDate());
        SupplierMaterial material = new SupplierMaterial();
        material.setCode(supplierMaterial.getCode());
        Optional<SupplierMaterial> first = supplierMaterialMapper.selectSupplierMaterialList(material).stream().findFirst();
        if (first.isPresent()) {
            int num = first.get().getProduction() + supplierMaterial.getProduction();
            first.get().setProduction(num);
            // TODO 更改现有库存
            return supplierMaterialMapper.updateSupplierMaterial(first.get());
        } else {
            return supplierMaterialMapper.insertSupplierMaterial(supplierMaterial);
        }
    }

    /**
     * 修改供应商物料库存
     *
     * @param supplierMaterial 供应商物料库存
     * @return 结果
     */
    @Override
    public int updateSupplierMaterial(SupplierMaterial supplierMaterial) {
        SupplierMaterial material = supplierMaterialMapper.selectSupplierMaterialById(supplierMaterial.getId());
        supplierMaterial.setExisting(supplierMaterial.getProduction() + material.getReturnMaterial() - material.getPurchase());
        supplierMaterial.setUpdateTime(DateUtils.getNowDate());
        return supplierMaterialMapper.updateSupplierMaterial(supplierMaterial);
    }

    /**
     * 批量删除供应商物料库存
     *
     * @param ids 需要删除的供应商物料库存主键
     * @return 结果
     */
    @Override
    public int deleteSupplierMaterialByIds(Long[] ids) {
        return supplierMaterialMapper.deleteSupplierMaterialByIds(ids);
    }

    /**
     * 删除供应商物料库存信息
     *
     * @param id 供应商物料库存主键
     * @return 结果
     */
    @Override
    public int deleteSupplierMaterialById(Long id) {
        return supplierMaterialMapper.deleteSupplierMaterialById(id);
    }
}
